package com.hnxxxy.sysmgr.service.impl;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.tools.Help;
import com.hnxxxy.common.tools.TableObj;
import com.hnxxxy.sysmgr.common.Constant;
import com.hnxxxy.sysmgr.mapper.RoleMapper;
import com.hnxxxy.sysmgr.mapper.StaffMapper;
import com.hnxxxy.sysmgr.pojo.Role;
import com.hnxxxy.sysmgr.pojo.Staff;
import com.hnxxxy.sysmgr.service.StaffService;
import com.hnxxxy.sysmgr.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Json
 * @date 2020-26-8:07
 */
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询所有员工
     * @param staffVo
     * @return
     */
    @Override
    public List queryAllStaff(StaffVo staffVo) {
        List<Staff> data = this.staffMapper.queryAllStaff(staffVo);
        return data;
    }

    /**
     * 添加员工
     * @param staffVo
     */
    @Override
    public Integer addStaff(StaffVo staffVo) {
        try {
            staffMapper.insertStaff(staffVo);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }

    }

    /**
     *查询所有员工，并返回树形结构
     * @param staffVo
     * @return
     */
    @Override
    public List<Staff> queryAllStaffForList(StaffVo staffVo) {

        return staffMapper.queryAllStaff(staffVo);
    }

    /**
     * 修改员工
     * @param staffVo
     */
    @Override
    public Integer updateStaff(StaffVo staffVo) {
        try {
            staffMapper.updateStffa(staffVo);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 删除员工
     * @param id
     */
    @Override
    public Integer deleteStaff(Integer id) {

        //根据用户id删除sys_staff_role中间表数据
            roleMapper.deleteRoleStaffByStaffid(id);

        try {
            staffMapper.deleteStaff(id);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 批量删除员工
     * @param ids
     */
    @Override
    public Integer batchDeletestaff(Integer[] ids) {
       try {
           for (Integer id : ids) {
               deleteStaff(id);
           }
           return AjaxRtnEntity.success;
       }catch (Exception e){
           e.printStackTrace();
           return AjaxRtnEntity.error;
       }
    }

    /**
     * 重置密码
     * @param staffVo
     */
    @Override
    public Integer updateStaffByStaffId(StaffVo staffVo) {

        try {
            staffMapper.updateStffa(staffVo);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 加载员功管理的分配角色的数据
     * @param id
     * @return
     */
    @Override
    public TableObj queryStaffRole(Integer id) {

        Role role = new Role();

        role.setState(Constant.AVAILABLE_FALSE);
        //查询所有角色
        List<Role> allRole = this.roleMapper.queryAllRole(role);
        //查询当前用户所拥有的角色
        List<Role> staffRole = this.roleMapper.queryRoleByStaffid(Constant.AVAILABLE_FALSE, id);

        List<Map<String,Object>> data = new ArrayList<>();
        for (Role role1 : allRole) {
            Boolean LAY_CHECKED=false;
            for( Role role2 : staffRole) {
                if (role1.getId().equals(role2.getId())){
                    LAY_CHECKED=true;
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("id",role1.getId());
            map.put("rolename",role1.getRolename());
            map.put("remark",role1.getRemark());
            map.put("LAY_CHECKED",LAY_CHECKED);

            data.add(map);
        }
        return new TableObj(data);
    }

    /**
     * 保存用户和角色的关系
     * @param staffVo
     */
    @Override
    public Integer saveStaffRole(StaffVo staffVo) {
        Integer staffid = staffVo.getId();
        Integer[] roleIds = staffVo.getIds();
        //根据用户id删除sys_staff_role里面的数据
        this.roleMapper.deleteRoleStaffByStaffid(staffid);
        try {
            //保存关系
            if(roleIds!=null && roleIds.length>0) {
                for (Integer rid : roleIds) {
                    this.staffMapper.insertStaffRole(staffid,rid);
                }
            }
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    @Override
    public Integer updatePwdByStaffId(StaffVo staffVo) {
        try {
            staffMapper.updateStffa(staffVo);

            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }
}
